<template>
  <div class="pieChart">
    <div class="total">
      <span>合计：</span>
      <span>{{ pieData.totalAll }}</span>
    </div>
    <div :id="pieData.pieId" :style="{ height: height, width: width }"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
echarts.use([PieChart]);
export default {
  components: {},
  props: {
    // 饼图数据
    pieData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      width: "100%",
      height: "240px",
      chart: null,
    };
  },
  created() {},
  mounted() {
    setTimeout(() => {
      this.pieCharts(this.pieData);
    }, 500);
  },
  beforeDestroy() {
    this.chartDes();
  },
  watch: {
    pieData: {
      handler(val) {
        if (val) {
          this.pieCharts(this.pieData);
        }
      },
      deep: true,
      immediate: false,
    },
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    pieCharts(pieData) {
      let option = {
        title: {
          text: pieData.title,
          left: "center",
          top: "0%",
        },
        tooltip: {
          trigger: "item",
          valueFormatter: function (value) {
            let unit = pieData.name === "电价图" ? "元" : "kwh";
            return value + unit;
          },
        },
        legend: {
          orient: "vertical",
          top: "20%",
          padding: 0,
          right: "1%",
          icon: "circle",
          itemGap: 3,
          textStyle: {
            color: "#8C8C8C",
            fontFamily: "PingFang-SC-Regular, PingFang-SC",
            fontWeight: 400,
            lineHeight: 17,
          },
        },
        series: [
          {
            name: pieData.name,
            type: "pie",
            minAngle: 5, //设置扇形的最小占比
            radius: ["50%", "70%"],
            // avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 1,
            },
            label: {
              show: false,
              position: "center",
              color: "#1890FF",
              formatter: function (params) {
                return params.value; // 中间显示数据
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "20",
                fontWeight: "bold",
              },
            },
            color: pieData.color,
            labelLine: {
              show: false,
            },
            data: pieData.dataArr,
          },
        ],
      };
      this.initChart(pieData.pieId, option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.pieChart {
  position: relative;
  .total {
    position: absolute;
    span {
      font-family: SourceHanSansSC;
      color: rgb(64, 149, 229);
      font-style: normal;
      letter-spacing: 0px;
      line-height: 40px;
      text-decoration: none;
      &:nth-child(1) {
        font-weight: 500;
        font-size: 24px;
      }
      &:nth-child(2) {
        font-weight: 700;
        font-size: 28px;
      }
    }
  }
}
</style>
